/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package boundaries;

import utils.ImageLoader;
import controllers.InvitationManager;
import controllers.NotificationManager;
import controllers.UserManager;
import entities.ChangeNotification;
import entities.DeleteNotification;
import entities.Invitation;
import entities.Notification;
import entities.OneDayNotification;
import entities.ThreeDaysNotification;
import entities.User;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.enterprise.context.RequestScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import org.primefaces.context.RequestContext;
import org.primefaces.event.CloseEvent;

/**
 *
 * @author Alberto, Alessandra
 */
@ManagedBean
@ViewScoped
public class UserBean {
    
    private static final Logger logger = Logger.getLogger("CalendarManager");

    @EJB
    private UserManager um;
    
    @EJB
    private InvitationManager im;
    
    @EJB
    private NotificationManager nm;
    
    private User user;
    
    private ArrayList<Invitation> newinvitations;
    private ArrayList<Notification> newnotifications;
    private ArrayList<Notification> notifications;
    
    private Notification currentnotification;

    public Notification getCurrentNotification() {
        return currentnotification;
    }

    public void setCurrentNotification(Notification currentNotification) {
        this.currentnotification = currentNotification;
    }

    public ArrayList<Invitation> getNewinvitations() {
        return newinvitations;
    }

    public void setNewinvitations(ArrayList<Invitation> newinvitations) {
        this.newinvitations = newinvitations;
    }

    public ArrayList<Notification> getNotifications() {
        return notifications;
    }

    public void setNotifications(ArrayList<Notification> notifications) {
        this.notifications = notifications;
    }
    
    public UserBean() {
    }
    
    public User getUser() {
        if (user == null) {
            user = um.getLoggedUser();
        }
        return user;
    }
    
    @PostConstruct
    public void init() {
        logger.log(Level.SEVERE, "creato userbean");
        this.newinvitations = new ArrayList<>();
        this.newnotifications = new ArrayList<>();
        this.notifications = new ArrayList<>();
        this.newnotifications.addAll(nm.getNewNotifications());
        this.notifications.addAll(nm.getNotifications());
        if (!newnotifications.isEmpty()) {
            currentnotification = newnotifications.get(0);
        } else {
            visualizeNewInvitations();
        }
    }
    
    public void visualizeNewNotification () {
        if (!newnotifications.isEmpty()) {
            currentnotification = newnotifications.get(0);
            if (currentnotification instanceof ChangeNotification) {
                nm.visualize(currentnotification);
                RequestContext context = RequestContext.getCurrentInstance();
                context.execute("PF('dialogChangeNotification').show();");
            }
            if (currentnotification instanceof DeleteNotification) {
                nm.deleteDeleteNotification((DeleteNotification)currentnotification);
                RequestContext context = RequestContext.getCurrentInstance();
                context.execute("PF('dialogDeleteNotification').show();");
            }
            if (currentnotification instanceof OneDayNotification) {
                nm.visualize(currentnotification);
                RequestContext context = RequestContext.getCurrentInstance();
                context.execute("PF('dialogOneDayNotification').show();");
            }
            if (currentnotification instanceof ThreeDaysNotification) {
                nm.visualize(currentnotification);
                RequestContext context = RequestContext.getCurrentInstance();
                context.execute("PF('dialogThreeDaysNotification').show();");
            }
            newnotifications.remove(currentnotification);
        } else {
            visualizeNewInvitations();
        }
    }
    
    public void visualizeNewInvitations () {
        this.newinvitations.addAll(im.getNewInvitations());
        if (!newinvitations.isEmpty()) {
            im.visualize(newinvitations);
            RequestContext context = RequestContext.getCurrentInstance();
            context.execute("PF('dialogInvitations').show();");
        }
    }
    
    public void handleClose() {
        visualizeNewNotification();
    }
    
    public void participateChangeNotification () {
        ChangeNotification not = (ChangeNotification) currentnotification;
        nm.participateInvitationAfterchange(not);
        RequestContext context = RequestContext.getCurrentInstance();
        context.execute("PF('dialogChangeNotification').hide();");
    }
    
    public void declineChangeNotification () {
        ChangeNotification not = (ChangeNotification) currentnotification;
        nm.declineInvitationAfterchange(not);
        RequestContext context = RequestContext.getCurrentInstance();
        context.execute("PF('dialogChangeNotification').hide();");
    }
    
    public void acceptProposedDate () {
        ThreeDaysNotification not = (ThreeDaysNotification) currentnotification;
        nm.rescheduleEvent(not);
        RequestContext context = RequestContext.getCurrentInstance();
        context.execute("PF('dialogThreeDaysNotification').hide();");
    }
    
    public void declineProposedDate () {
        RequestContext context = RequestContext.getCurrentInstance();
        context.execute("PF('dialogThreeDaysNotification').hide();");
    }
    
    public String getWeatherOfOneDayNotification () {
        if (currentnotification instanceof OneDayNotification) {
            return ((OneDayNotification) currentnotification).getEvent().getWeatherforecast().getName().toString();
        } else {
            return "";
        }
    }
    
    public String getWeatherOfThreeDaysNotification () {
        if (currentnotification instanceof ThreeDaysNotification) {
            return ((ThreeDaysNotification) currentnotification).getEvent().getWeatherforecast().getName().toString();
        } else {
            return "";
        }
    }
    
    public String getWeatherForecast () {
        if (currentnotification instanceof ThreeDaysNotification ) {
            return ImageLoader.getWeatherImg(((ThreeDaysNotification)currentnotification).getEvent().getWeatherforecast().getName());
        } else if (currentnotification instanceof OneDayNotification) {
            return ImageLoader.getWeatherImg(((OneDayNotification)currentnotification).getEvent().getWeatherforecast().getName());  
        } else {
            return "";
        }
    }
    
    public String getDateOfThreeDaysNotification () {
        if (currentnotification instanceof ThreeDaysNotification && ((ThreeDaysNotification) currentnotification).hasProposedDate()) {
            return ((ThreeDaysNotification) currentnotification).getProposeddate().toString();
        } else {
            return "";
        }
    }
    
    public Boolean hasDateThreeDaysNotification () {
        if (currentnotification instanceof ThreeDaysNotification) {
            return ((ThreeDaysNotification) currentnotification).hasProposedDate();
        } else {
            return false;
        }
    }
}
